package com.care.dao.knowledge;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.care.entity.Doctor;
import com.care.entity.Knowledge;
import com.sun.glass.events.GestureEvent;

@Repository
public class KnowledgeDaoImpl {
	@Resource
	private SessionFactory sessionFactory;
	public Session getSession(){
		return this.sessionFactory.getCurrentSession();		
	}
	/***
	 * 添加医护知识
	 * @param knowledge
	 */
	public void AddKnowledgeByDoctor(Knowledge knowledge,Doctor doctor){
		knowledge.setContent(knowledge.getContent());
		knowledge.setTitle(knowledge.getTitle());
		knowledge.setDoctor(doctor);		
		knowledge.setImg(knowledge.getImg());
		getSession().save(knowledge);				
	}
	/**
	 * 对titile属性去重 
	 * @param knowledge
	 * @return
	 */
	public boolean UniqueKnowledge(Knowledge knowledge){
		String hql="from Knowledge where title=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, knowledge.getTitle());
		
		if (query.uniqueResult()==null) {
			return true;
			
		}
		return false;
		
	}
	/**
	 * 医护知识分页
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Knowledge> FindKnowledgeByPage(int pageNum,int pageSize){
		String hql="from Knowledge ";
		Query query=getSession().createQuery(hql);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);			
		return query.list();				
	}
	/**
	 * 计算总条数
	 * @return
	 */
	public int CountKnowledge(){
		String hql="select count(*) from Knowledge";
		Query query=getSession().createQuery(hql);
		int count=((Long) query.setCacheable(true).uniqueResult()).intValue();
		return count;
		
	}
}
